package com.hna.eking.AirlineServer.security;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore; 

public class DeviceUser implements UserDetails{
	
	private static final long serialVersionUID = -6707696496117245444L;
	
	private String id;
    private String username;
    @JsonIgnore
    private String password;
    private String uuid;
    @JsonIgnore
    private List<GrantedAuthority> authorities;
    
    //这三个属性必须加上！！
    @JsonIgnore
    private  boolean accountNonExpired;
    @JsonIgnore
    private  boolean accountNonLocked;
    @JsonIgnore
    private  boolean credentialsNonExpired;
    
    public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public DeviceUser(){}
	
	 public DeviceUser(String username, String uuid, String password){
    	this.username = username;
    	this.password = password;
    	this.uuid = uuid;
	 }
    
    public DeviceUser(String id, String username, String uuid, String password, List<GrantedAuthority> authorities,
    		boolean accountNonExpired, boolean accountNonLocked, boolean credentialsNonExpired){
    	this.id = id;
    	this.username = username;
    	this.password = password;
    	this.uuid = uuid;
    	this.authorities = authorities;
    	this.accountNonExpired = accountNonExpired;
    	this.accountNonLocked = accountNonLocked;
    	this.credentialsNonExpired = credentialsNonExpired;
    }
    
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	@Override
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	@Override
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	
	@Override
	public boolean equals(Object rhs) {
		if (rhs instanceof DeviceUser) {
			return username.equals(((DeviceUser) rhs).username) 
					&& uuid.equals(((DeviceUser)rhs).getUuid());
		}
		return false;
	}
	
	@Override
    public int hashCode() {
        return (this.username + this.uuid ).hashCode();
    }
	
	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return accountNonExpired;
	}
	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return accountNonLocked;
	}
	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return credentialsNonExpired;
	}
	
	@Override
	@JsonIgnore
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}
	
	@Override
	public List<GrantedAuthority> getAuthorities() {
		return authorities;
	}
	
	
	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}
	
	public String toString() {
		return String.format("username: %s;uuid: %s; authority:%s",
				username, uuid, authorities.get(0).getAuthority());
	}
}
